<template>
  <basic-container>
    <el-form :inline="true" :model="query" class="demo-form-inline">
      <el-row>
        <el-form-item label="项目编号:">
          <el-input v-model="query.projectCode" clearable placeholder="项目编号" size="small"></el-input>
        </el-form-item>
        <el-form-item label="项目名称:">
          <el-input v-model="query.projectName" clearable placeholder="项目名称"></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-search" @click="onSearch" :loading="loading">搜索
          </el-button>
          <el-button size="small" icon="el-icon-delete" @click="searchReset" :loading="loading">清空</el-button>
        </el-form-item>
      </el-row>
    </el-form>
        <div class="btn-operate">
          <el-button  @click="createEvidence" type="primary" size="small" icon="el-icon-plus">
            问题整改
          </el-button>
        </div>
    <el-table v-loading="loading" :data="tableData" border
              highlight-current-row @cell-click="handleCellClick" style="width: 100%">
      <el-table-column prop="projectCode" label="项目编号"></el-table-column>
      <el-table-column prop="projectName" label="项目名称"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status === 0 || scope.row.status === null">新建</span>
          <span v-else-if="scope.row.status === 1">已发送</span>
          <span v-else-if="scope.row.status === 2">已生成整改报告</span>
          <span v-else>未知状态</span>
        </template>
      </el-table-column>
      <el-table-column prop="problemCount" label="问题总数">
      </el-table-column>
      <el-table-column label="操作" width="170">
        <template slot-scope="scope">
          <el-button type="text" size="small" icon="el-icon-view" @click="showViewBox(scope.row)">查看</el-button>
          <el-button v-if="permissions.rectificationNotice_get && scope.row.status === 0" type="text" size="small" icon="el-icon-edit" @click="showEditBox(scope.row)">编辑</el-button>
          <el-button v-if="permissions.rectifyReport_add && scope.row.status === 1" type="text" size="small" icon="el-icon-edit" @click="createReportBox(scope.row)">生成整改报告</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 30, 40 ,50 ,100]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <el-dialog title="整改通知书" fullscreen custom-class="ledger" :visible.sync="formBox" :close-on-click-modal="!formBox" :append-to-body="true" :before-close="resetForm" width="1200px" top="5vh">
      <div class="colums">
        <div class="colums_left">
          <div class="colums_left_middle">
            <div class="middleForm ledger">
              <el-form ref="optForm" :model="form" class="el-dialog__body" label-width="100px">
                <div class="title">项目信息</div>
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="项目名称:" prop="projectName">
                      <el-input v-model="form.projectName" placeholder="选择项目" @focus="showProjectDialog()" :readonly="true" :rules="{required: true, message: '项目名称不能为空', trigger: ['focus', 'change']}"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <div class="title">基本信息</div>
                <div style="margin: 0 auto; width: 840px;">
                  <el-row>
                    <el-col :span="5">
                      <el-form-item label-width="0">
                        <el-input v-model="form.auditedCompanyName" disabled>
                          <template slot="append">:</template>
                        </el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row>
                    <el-col>
                      <el-form-item label-width="0">
                        <el-input class="custom-textarea" v-model="form.content" placeholder="填写模板内容" type="textarea" autosize :rules="{required: true, message: '模板内容不能为空', trigger: ['focus', 'change']}">
                        </el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="19">
                      <el-form-item>
                        <div style="margin-left: 28px;">

                        </div>
                      </el-form-item>
                    </el-col>
                    <el-col :span="19">
                      <el-form-item>

                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row>
                    <el-col style="display: flex; justify-content: flex-end;">
                      审计处
                    </el-col>
                  </el-row>
                  <el-row>
                    <el-col style="display: flex; justify-content: flex-end;">
                      {{form.createDate}}
                    </el-col>
                  </el-row>
                </div>
                <div class="title" v-if="form.sendDate">整改通知书</div>
                <el-row>
                  <el-col>
                    <el-form-item label="整改通知书:" v-if="form.sendDate">
                      <web-document :form-data="form" :readWord="'edit'" :pageType="'rectifyNotice'" @createWordFile="wordFileHandle" v-if="form.sendDate"/>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
              <span slot="footer" class="dialog-footer">
                <el-button @click="resetForm" size="small" icon="el-icon-close">关闭</el-button>
        <!--        <el-button v-if="!isView && form.isSend !== 1" type="primary" @click="submit" size="small" icon="el-icon-check">保存</el-button>-->
                <el-button v-if="!isView && (!form.status || form.status === 0)" type="primary" @click="send" size="small" icon="el-icon-check">发送</el-button>
              </span>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
    <el-dialog title="整改通知书" fullscreen custom-class="ledger" :visible.sync="newBox" :close-on-click-modal="!newBox" :append-to-body="true" :before-close="resetForm" width="1200px" top="5vh">
      <div class="colums">
        <div class="colums_left">
          <div class="colums_left_middle">
            <div class="middleForm ledger">
              <el-form ref="optForm" :model="form" class="el-dialog__body" label-width="100px">
                <div class="ledgerTitle">项目信息</div>
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="项目信息: " prop="projectName">
                      {{form.projectName}}
                    </el-form-item>
                  </el-col>
                </el-row>
                <div class="ledgerTitle">基本信息</div>
                <div style="margin: 0 auto; width: 945px;">
                  <el-row style="font-size: 15px; line-height: 2.6;">
                    {{ form.auditedCompanyName }}:<br/>
                    &ensp;&ensp;&ensp;&ensp;{{form.content}}
                  </el-row>
                  <el-row>
                    <el-col style="display: flex; justify-content: flex-end;">
                      审计处
                    </el-col>
                  </el-row>
                  <el-row>
                    <el-col style="display: flex; justify-content: flex-end;">
                      {{form.createDate}}
                    </el-col>
                  </el-row>
                </div>
                <div class="title" v-if="form.sendDate">整改通知书</div>
                <el-row>
                  <el-col>
                    <el-form-item label="整改通知书:" v-if="form.sendDate">
                      <web-document :form-data="form" :readWord="'edit'" :pageType="'rectifyNotice'" @createWordFile="wordFileHandle" v-if="form.sendDate"/>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
              <span slot="footer" class="dialog-footer">
                <el-button @click="resetForm" size="small" icon="el-icon-close">关闭</el-button>
        <!--        <el-button v-if="!isView && form.isSend !== 1" type="primary" @click="submit" size="small" icon="el-icon-check">保存</el-button>-->
                <el-button v-if="!isView && form.status === 1" type="primary" @click="createReport" size="small" icon="el-icon-check">填写整改报告</el-button>
              </span>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>

    <el-dialog top="5vh" :title="formTitle" :visible.sync="viewBox"
               :append-to-body="true" :before-close="closeViewBox" width="1200px">
      <RectifyView :params="form"></RectifyView>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeViewBox" size="small" icon="el-icon-close">关闭</el-button>
      </span>
    </el-dialog>

    <ChoseProject :projectDialog="projectDialog" @cancelDialog="closeProjectDialog" @setValue="projectDataHandle" ref="ChoseProject"/>
    <table-form ref="reportForm" @cancelDialog="closeReportFormDialog"></table-form>
  </basic-container>
</template>

<script>
import {mapGetters} from "vuex";
import {fetchList,submit,send,getNotice} from "@/api/adis/track/rectify";
import ChoseProject from "@/components/audited/ChoseProject.vue";
import TableForm from "@/components/form/rectifyReprotForm.vue";
import RectifyView from "@/components/actForm/RectifyView.vue";
import WebDocument from "@/components/form/webDocument.vue";
import Template from "@/views/adis/base/template";

export default {
  components: {Template, WebDocument, RectifyView, TableForm, ChoseProject},
  computed:{
    ...mapGetters(["permissions"])
  },
  data() {
    return {
      query:{},
      formTitle:'',
      tableData: [],
      form:{
        createDate : `${new Date().getFullYear()}年${new Date().getMonth() + 1}月${new Date().getDate()}日`
      },
      loading:false,
      formBox:false,
      viewBox:false,
      newBox:false,
      isView:false,
      projectDialog:false,
      page:{
        total: 1,
        currentPage:10
      },
    }
  },
  created() {
    this.onLoad(this.page);
    this.Authorization = 'Bearer ' + this.$store.getters.access_token
  },
  methods:{
    handleCellClick(row, column) {
      if (row && column.label !== "操作"){
        this.showViewBox(row)
      }
    },

    createReport(){
      this.$nextTick(() => {
        this.$refs.reportForm.init(this.form,true);
      })
      setTimeout(() => {
        this.newBox = false;
      }, 500)
    },
    closeReportFormDialog(){
      this.form = {}
      this.onLoad(this.page);
    },

    showProjectDialog() {
      this.projectDialog = true
      this.$refs.ChoseProject.initParams({
        hasNotReport: false ,// 已生成审计报告
        hasNotRectify: true,// 未生成整改通知书
        isTermination: false,
        isEngineering: false,//是否工程审计
      });
    },
    closeProjectDialog() {
      this.projectDialog = false
      console.log(this.form)
    },
    projectDataHandle(row){
      this.projectDialog = false;
      this.projectForm = row;
      this.form.auditedCompanyName = this.projectForm.auditedCompanyName;
      this.form.projectId = this.projectForm.projectId;
      this.form.projectName = this.projectForm.projectName;
    },
    sizeChange(size) {
      this.page.size = size;
      this.onLoad(this.page);
    },
    currentChange(current) {
      this.page.current = current;
      this.onLoad(this.page);
    },
    onSearch(){
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchReset() {
      this.query = {};
      this.page.current = 1;
      this.onLoad(this.page)
    },
    onLoad(page, params = {}){
      this.loading = true;
      fetchList(Object.assign(params, this.query, page)).then(res => {
        this.loading = false;
        const data = res.data.data;
        this.page.total = data.total;
        this.tableData = data.records;
      }, () => {
        this.loading = false;
      });
    },
    showViewBox(row){
      getNotice(row.rectifyNoticeId).then((res) => {
        this.form = res.data.data
        this.formTitle = row.projectName
        this.viewBox = true;
      })
    },
    closeViewBox() {
      this.viewBox = false;
      this.resetForm();
    },
    showEditBox(row){
      getNotice(row.rectifyNoticeId).then((res)=>{
        this.form = res.data.data;
        const dateObj = new Date(this.form.sendDate);
        this.form.createDate = `${dateObj.getFullYear()}年${dateObj.getMonth() + 1}月${dateObj.getDate()}日`
        this.formBox=true;
      })
    },
    createReportBox(row){
      getNotice(row.rectifyNoticeId).then((res)=>{
        this.form = res.data.data;
        const dateObj = new Date(this.form.sendDate);
        this.form.createDate = `${dateObj.getFullYear()}年${dateObj.getMonth() + 1}月${dateObj.getDate()}日`
        this.newBox = true;
      })
    },
    createEvidence(){
      this.formBox=true;
      this.form.createDate = `${new Date().getFullYear()}年${new Date().getMonth() + 1}月${new Date().getDate()}日`
    },
    resetForm(){
      this.formBox=false;
      this.newBox=false;
      this.form = {};
      this.form.createDate = `${new Date().getFullYear()}年${new Date().getMonth() + 1}月${new Date().getDate()}日`
      this.$refs['optForm'] && this.$refs['optForm'].resetFields();
      this.isView =false;
    },
    submit(){
      this.$refs['optForm'].validate((valid) => {
        if (valid) {
          this.loading = true;
          submit(this.form).then((res) => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.resetForm();
            this.onLoad(this.page);
          }, () => {
            this.loading = false;
          });
        } else {
          return false;
        }
      });
    },
    send(){
      this.$refs['optForm'].validate((valid) => {
        if (valid) {
          this.loading = true;
          send(this.form).then((res) => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.resetForm();
            this.onLoad(this.page);
          }, () => {
            this.loading = false;
          });
        } else {
          return false;
        }
      });
    },
    wordFileHandle(e) {
      this.$set(this.form, "wordFile", e.wordFile);
      this.$set(this.form, "reportId", e.id);
    }
  }
}
</script>
<style scoped>
.custom-textarea /deep/ textarea {
  font-size: 15px; /* 设置字体大小 */
  color: #333; /* 设置字体颜色 */
}
</style>
